package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.ProcessStep;
import com.qidianit.mes.pro.mapper.ProcessStepMapper;
import com.qidianit.mes.pro.service.ProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工序步骤服务实现类
 */
@Service
public class ProcessStepServiceImpl implements ProcessStepService {

    @Autowired
    private ProcessStepMapper processStepMapper;

    @Override
    public Result<PageInfo<ProcessStep>> selectList(ProcessStep processStep, int pageNum, int pageSize) {
        try {
            // 开启分页
            PageHelper.startPage(pageNum, pageSize);
            // 执行查询
            List<ProcessStep> stepList = processStepMapper.selectList(processStep.getProcessId());
            // 封装分页结果
            PageInfo<ProcessStep> pageInfo = new PageInfo<>(stepList);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询工序步骤列表失败：" + e.getMessage());
        }
    }

    @Override
    public Result<ProcessStep> selectById(Long id) {
        try {
            if (id == null) {
                return Result.error("ID不能为空");
            }
            ProcessStep processStep = processStepMapper.selectById(id);
            if (processStep != null) {
                return Result.success(processStep);
            } else {
                return Result.error("未找到对应的工序步骤");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询工序步骤失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<ProcessStep>> selectByProcessId(Long processId) {
        try {
            if (processId == null) {
                return Result.error("工序ID不能为空");
            }
            List<ProcessStep> stepList = processStepMapper.selectList(processId);
            return Result.success(stepList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询工序下的步骤列表失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public Result saveStep(ProcessStep processStep) {
        try {
            // 校验必要参数
            if (processStep.getProcessId() == null) {
                return Result.error("关联工序ID不能为空");
            }
            if (processStep.getStepOrder() == null) {
                return Result.error("步骤顺序不能为空");
            }

            // 执行保存操作
            int rows = processStepMapper.saveStep(processStep);
            if (rows > 0) {
                return Result.success("新增工序步骤成功");
            } else {
                return Result.error("新增工序步骤失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增工序步骤失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public Result updateById(ProcessStep processStep) {
        try {
            // 校验必要参数
            if (processStep.getId() == null) {
                return Result.error("ID不能为空");
            }


            // 执行更新操作
            int rows = processStepMapper.updateById(processStep);
            if (rows > 0) {
                return Result.success("更新工序步骤成功");
            } else {
                return Result.error("更新工序步骤失败，未找到对应的记录或数据未发生变化");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新工序步骤失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public Result deleteById(Long id) {
        try {
            if (id == null) {
                return Result.error("ID不能为空");
            }

            int rows = processStepMapper.deleteById(id);
            if (rows > 0) {
                return Result.success("删除工序步骤成功");
            } else {
                return Result.error("删除工序步骤失败，未找到对应的记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除工序步骤失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public Result batchDelete(Long[] ids) {
        try {
            if (ids == null || ids.length == 0) {
                return Result.error("请选择要删除的工序步骤");
            }

            int deleteCount = 0;
            for (Long id : ids) {
                // 循环删除每个ID对应的记录
                int rows = processStepMapper.deleteById(id);
                deleteCount += rows;
            }

            if (deleteCount > 0) {
                return Result.success("批量删除成功，共删除" + deleteCount + "条记录");
            } else {
                return Result.error("批量删除失败，未找到对应的记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除工序步骤失败：" + e.getMessage());
        }
    }
}
